package xin.marcher.module.quality.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.exception.ProductBizException;
import xin.marcher.module.quality.domain.request.SurveyWhiteListRequest;
import xin.marcher.module.quality.service.ItemQualityControlWhiteListService;
import xin.marcher.module.rights.support.LoginUser;
import xin.marcher.module.rights.support.LoginUserHolder;

import java.util.Objects;

/**
 *
 */
@RestController
@RequestMapping("/quality/whitelist")
@Slf4j
public class ItemQualityControlWhiteListController {
    @Autowired
    private ItemQualityControlWhiteListService whiteListService;

    /**
     * 查询白名单列表
     *
     * @param token
     * @param request
     * @return
     */
    @PreAuthorize("@rights.check('qualityWhitelistList',#token)")
    @GetMapping("/list")
    JsonResult qualityWhiteList(String token, @RequestBody SurveyWhiteListRequest request) {
        try {
            LoginUser user = LoginUserHolder.getLoginUser();
            if (Objects.isNull(user.getUserId()) || Objects.isNull(user.getUsername())) {
                return JsonResult.buildError("用户登录出错");
            }
            PageResult pageResult = whiteListService.qualityWhiteList(request);
            return JsonResult.buildSuccess(pageResult.getContent());
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 变更白名单
     *
     * @param token
     * @param request
     * @return
     */
    @PreAuthorize("@rights.check('qualityWhitelistChange',#token)")
    @PostMapping("/change")
    JsonResult changeQualityWhite(String token, @RequestBody SurveyWhiteListRequest request) {
        try {
            LoginUser user = LoginUserHolder.getLoginUser();
            if (Objects.isNull(user.getUserId()) || Objects.isNull(user.getUsername())) {
                return JsonResult.buildError("用户登录出错");
            }
            whiteListService.changeQualityWhite(request);
            return JsonResult.buildSuccess();
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

}
